<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="124px">
      <el-form-item label="提现人姓名" prop="userName" label-width="100px">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入提现人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属代理商id" prop="proxyId">
        <el-input
          v-model="queryParams.proxyId"
          placeholder="请输入所属代理商id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>      
     <el-form-item label="状态" prop="status" label-width="70px">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:branchApply:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyList">
      <!-- <el-table-column label="开户行" align="center" prop="bankDeposit" />
      <el-table-column label="银行卡号" align="center" prop="bankNumber" />
      <el-table-column label="银行卡预留手机号" align="center" prop="bankPhone" />
      <el-table-column label="	支行名称" align="center" prop="branchName" />
      <el-table-column label="id" align="center" prop="id" /> -->
      <!-- <el-table-column label="银行卡负责人姓名" align="center" prop="principalName" />
      <el-table-column label="银行卡负责人手机号" align="center" prop="principalPhone" />
      <el-table-column label="所属商户id" align="center" prop="shopId" /> -->
      <el-table-column label="提现人id" align="center" prop="userId" />
      <el-table-column label="提现人姓名" align="center" prop="userName" />      
      <el-table-column label="提现金额" align="center" prop="withdrawAmount" />
      <el-table-column label="提现完成时间" align="center" prop="finishTime">
        <template slot-scope="scope">
          <span v-if="scope.row.status ==1">{{scope.row.finishTime}}</span>
          <span v-if="scope.row.status ==2">/</span>
        </template>
      </el-table-column>
      <el-table-column label="驳回理由" align="center" prop="rejectReason">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1">/</span>
          <span v-if="scope.row.status == 2">{{scope.row.rejectReason}}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="申请状态" align="center" prop="status" >
          <template slot-scope="scope">
              <span v-if="scope.row.status ==0">受理中</span>
              <span v-if="scope.row.status ==1">完成</span>
              <span v-if="scope.row.status ==2">驳回</span>
          </template>
      </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleDetail(scope.row)"
                v-hasPermi="['system:branchApply:query']"
            >查看详情</el-button>
            <el-button
              size="mini"
              type="text"
              v-if="scope.row.status == 0"
              @click="handleExamine(scope.row)"
              v-hasPermi="['system:goods:edit']"
            >审核</el-button>
         </template>
        </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--查看详情弹出层-->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
        <el-descriptions :column="2" :colon="false">
            <el-descriptions-item><span style="font-size: 15px; color:#1890ff">真实姓名 :</span>&emsp;{{applyDetail.realName}}</el-descriptions-item>
            <el-descriptions-item><span style="font-size: 15px; color:#1890ff">身份证号码 :</span>&emsp;{{applyDetail.idCard}}</el-descriptions-item>
            <el-descriptions-item><span style="font-size: 15px; color:#1890ff">开户行 :</span>&emsp;{{applyDetail.bankDeposit}}</el-descriptions-item>
            <el-descriptions-item><span style="font-size: 15px; color:#1890ff">支行名称 :</span>&emsp;{{applyDetail.branchName}}</el-descriptions-item>
            <el-descriptions-item><span style="font-size: 15px; color:#1890ff">银行卡号 :</span>&emsp;{{applyDetail.bankNumber}}</el-descriptions-item>
            <el-descriptions-item><span style="font-size: 15px; color:#1890ff">绑定手机号 :</span>&emsp;{{applyDetail.bankPhone}}</el-descriptions-item>
        </el-descriptions>
    </el-dialog>
    <!--审核提现弹出层-->
      <el-dialog :title="title" :visible.sync="examineOpen" width="650px" append-to-body>
        <el-form ref="form" :model="examineForm" label-width="140px">
          <el-form-item label="关联的银行卡id :" prop="bankId">
            <span>{{examineForm.bankId}}</span>
          </el-form-item>
          <el-form-item label="所属代理商id :" prop="proxyId">
            <span>{{examineForm.proxyId}}</span>
          </el-form-item>
          <el-form-item label="提现人姓名 :" prop="userName">
            <span>{{examineForm.userName}}</span>
          </el-form-item>
          <el-form-item label="提现人id :" prop="userId">
            <span>{{examineForm.userId}}</span>
          </el-form-item>
          <el-form-item label="提现金额 :" prop="withdrawAmount">
            <span>{{examineForm.withdrawAmount}}</span>
          </el-form-item>
          <!-- <el-form-item label="提现完成时间 :" prop="finishTime">
            <span>{{examineForm.finishTime}}</span>
          </el-form-item>
          <el-form-item label="备注 :" prop="remark">
            <span>{{examineForm.remark}}</span>
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitExamin" style="float: left">通  过</el-button>
          <el-button @click="cancelExamin" >驳  回</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
import { getWithdrawal, detailWithdrawal} from "@/api/acting/withdrawal";
import { downLoadExcel } from "@/utils/download";
import {examine} from '@/api/acting/withdrawal';
export default {
  name: "Record",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 代理商申请提现记录表格数据
      applyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        subRole: undefined,
        goodName: undefined,
        goodId: undefined,
        totalAmount: undefined,
        orderNum: undefined,
        userId: undefined,
        publicName: undefined,
        rebateAmount: undefined,
        publicId: undefined,
      },
      // 表单参数
      form: {},
      //申请提现详情
      applyDetail: {},
      //是否弹出审核层
      examineOpen: false,
      //审核表单
      examineForm: {},
      //申请提现列表
      examineDetail: {},
      // 状态数据字典
      statusOptions: [
        { dictLabel: "受理中", dictValue: "0" },
        { dictLabel: "完成", dictValue: "1" },
        { dictLabel: '驳回', dictValue: '2'}
      ],
      // 表单校验
      rules: {
        subRole: [
          { required: true, message: "1代理商给代理商 2 商家给代理商不能为空", trigger: "blur" }
        ],
        goodId: [
          { required: true, message: "商品id不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        publicId: [
          { required: true, message: "如果role为1 折此字段是代理商id 2此字段为商家id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询分销返利记录列表 */
    getList() {
      this.loading = true;
      getWithdrawal(this.queryParams).then(response => {
          console.log(response.rows)
        this.applyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    // cancel() {
    //   this.open = false;
    //   this.reset();
    // },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        subRole: undefined,
        goodName: undefined,
        goodId: undefined,
        totalAmount: undefined,
        orderNum: undefined,
        userId: undefined,
        publicName: undefined,
        rebateAmount: undefined,
        publicId: undefined,
        createTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询详情按钮操作 */
    handleDetail(row) {
      this.open = true
      detailWithdrawal(row.id).then(response => {
        this.applyDetail = response.data.bankCard
        this.title = "查询代理商申请提现详情";
      });
    },
    /**审核按钮操作 */
    handleExamine(row) {
      detailWithdrawal(row.id).then(response => {
        console.log(response.data)
        this.examineForm = response.data;
        this.examineOpen = true;
        this.title = '审核提现'
      })
    },
    /** 通过按钮 */
    submitExamin: function () {
            const examineId = this.examineForm.id;
            examine({id: examineId, status: 1, context: '通过'}).then((response) => {
              this.msgSuccess('审核通过');
              this.getList();
              this.examineOpen = false;
            })
    }, 
    // 驳回按钮
    cancelExamin() {
       const examineId = this.examineForm.id || this.ids;
       this.$prompt('请输入的驳回理由', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputPattern: /^.{5,50}$/,
        inputErrorMessage: "驳回内容不能小于5个字大于50个字",
      }).then(({ value }) => {
      examine({
          id: examineId,
          status:2,
          rejectReason:value
      }).then((response) => {
        this.msgSuccess("驳回成功");
        this.examineOpen = false;
        // this.showbtn = false;
        this.getList();
      })
      }).catch(() => {});

    },
    /** 导出按钮操作 */
    handleExport() {
      downLoadExcel('/system/branch/apply/export', this.queryParams);
    }
  }
};
</script>
